import { Fenxiao } from './fenxiao-model.js';
import { Sms } from '../../utils/sms.js';
var fenxiao = new Fenxiao();
var sms = new Sms();
Page({
  data: {
    show_send_code: true,
    countDownNum:60,
    args:{},
    btnType:'disable',
    btnText:"立即开通",
    onLoadding:false
  },
  onLoad: function (options) {
    var userInfo = wx.getStorageSync('userInfo');
    this.setData({ 
      userInfo,
      'args.mobile': userInfo.mobile
    })
  },
  //提交表单
  formSubmit: function (e) {
    if (!this.data.args.mobile || !this.data.args.code) {
      wx.showToast({
        title: '请先填写手机号和验证码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.sub();
  },
  //开通分销商
  sub: function () {
    var that = this;
    var params =  that.data.args;
    if(this.data.onLoadding){
      return false;
    }
    this.setData({btnText:'正在开通...'})
    this.data.onLoadding = true;
    fenxiao.register(params, (data) => {
      that.data.onLoadding = false;
      this.setData({ btnText: '立即开通' })
      if (data.status == 1) {
        wx.showToast({
          title: "开通成功",
          icon: 'success',
          duration: 2000,
          success:function(){
            var user = wx.getStorageSync('userInfo');
            user.is_distribut = 1;
            wx.setStorageSync('userInfo', user);
            var url = "../center/center";
            setTimeout(function () {
              wx.switchTab({
                url: url,
              })
            }, 2000)
          }
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  //发送短信验证码
  sendCode: function (e) {
    if (!this.data.args.mobile){
      wx.showToast({
        title: '请先填写手机号码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      show_send_code : false,
      countDownNum : 60
    });
    this.countDown();
    var params ={
      mobile : this.data.args.mobile,
      scene : 9
    }
    sms.send(params, (data) => {
      if(data.status == 1){
        wx.showToast({
          title: "验证码已发送到您手机上",
          icon: 'none',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;//获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          clearInterval(that.data.timer);
          that.setData({
            show_send_code: true
          });
        }
      }, 1000)
    })
  },
  //获取用户输入的手机号码内容
  mobileChange: function (e) {
    var val = e.detail.value;
    this.setData({
      'args.mobile': val
    });
    this.btnChange();
  },
  //获取用户输入的验证码内容
  codeChange: function (e) {
    var val = e.detail.value;
    this.setData({
      'args.code': val
    });
    this.btnChange();

  },
  //判断激活按钮的样式
  btnChange: function () {
    let type = 'disable'
    if (this.data.args.mobile && this.data.args.code) {
      type = 'sub';
    }
    this.setData({
      btnType: type
    });
  }
})